﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Web.System.Services.Auths;
using SaaS.Core.Authorization;
using Microsoft.AspNetCore.Mvc;
using XCore.Extensions;
using XCore.Logger;
using SaaS.Core;
using XCore.Authorization;
using System.ComponentModel.DataAnnotations;

namespace Web.System.Controllers.Systems
{
    /// <summary>
    /// 角色
    /// </summary>
    [Route("api/system/[controller]/[action]")]
    [ApiController]
    public class RoleController : SystemControllerBase
    {
        private readonly RoleAppService _roleAppService;
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="roleAppService"></param>
        public RoleController(RoleAppService roleAppService)
        {
            _roleAppService = roleAppService;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost, Permission(PermissionNames.Permission_Role_Create)]
        public ActionResult Save([FromForm]int? id, [FromForm, Required]string name)
        {
            return Ok(_roleAppService.CreateOrEdit(id ?? 0, name));
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), Permission(PermissionNames.Permission_Role)]
        public ActionResult Info(int id)
        {
            if (!SDC.SystemRoles.Any(c => c.Id == id)) throw new MessageException(CoreSetting.Error_ID);
            return Ok(SDC.SystemRoles.Where(c => c.Id == id).Select(c => new
            {
                c.Name,
                c.Id,
            }).FirstOrDefault());
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}"), Permission(PermissionNames.Permission_Role_Delete)]
        public ActionResult Delete(int id)
        {
            return Ok(_roleAppService.Delete(id));
        }
        /// <summary>
        /// 角色权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}"), Permission(PermissionNames.Permission_Role)]
        public ActionResult Permissions(int id)
        {
            return Ok(_roleAppService.GetPermissions(id));
        }
        /// <summary>
        /// 获取角色字典
        /// </summary>
        /// <returns></returns>
        [HttpGet, Permission(PermissionNames.Permission_Role)]
        public ActionResult Dictionary()
        {
            return Ok(_roleAppService.GetDictionary());
        }
        /// <summary>
        /// 授权
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("{id}"), Permission(PermissionNames.Permission_Role_Authoriza)]
        public ActionResult Authoriza(int id, string[] permissions)
        {
            return Ok(_roleAppService.Authoriza(id, permissions, new PermissionAuthorizationProvider(), new PermissionSystemProvider(), new PermissionBlogProvider()));
        }

    }
}
